//
//  WWTableBarController.swift
//  SinWEiBo
//
//  Created by 胡良志 on 2018/3/10.
//  Copyright © 2018年 HuLiangZhi. All rights reserved.
//

import UIKit

class WWTableBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        let wbBtn = WWTabBar()
//        self.tabBar = wbBtn  由于Tabbar是只读属性,不能直接赋值,可以间接赋值用KVC
        self .setValue(wbBtn, forKey: "tabBar")
        
        wbBtn.composeClick = {
//            let demo = WWDemo_1_Controller()
//            demo.view.backgroundColor = #colorLiteral(red: 0.5725490451, green: 0, blue: 0.2313725501, alpha: 1)
//            let nav = UINavigationController(rootViewController: demo)
//            demo.modalPresentationStyle = .formSheet
//            self.show(nav, sender: true)
            let compose = WWComposeView()
            compose.show(vc: self)
//            UIApplication.shared.keyWindow?.addSubview(compose)
        }
        
        self .addchildViewcontrollers()
    }

    //MARK: 添加四个子控制器
   fileprivate func addchildViewcontrollers(){
    addchildViewcontroller(vc: WWHomeViewController(), title: "首页", imageName: "tabbar_home",index: 0)
    addchildViewcontroller(vc: WWMessageController(), title: "消息", imageName: "tabbar_message_center",index: 1)
    addchildViewcontroller(vc: WWDiscoverController(), title: "发现", imageName: "tabbar_discover",index: 2)
    addchildViewcontroller(vc: WWProfileController(), title: "我的", imageName: "tabbar_profile",index: 3)
    }

    //封装四个tabbar按钮
    fileprivate func addchildViewcontroller (vc:UIViewController,title:String,imageName:String,index:Int){
        vc.title = title
        vc.tabBarItem.tag = index
        //需要同时设置item和Image才能显示
        vc.tabBarItem.image = UIImage(named: imageName)
        //设置选中图片
        vc.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
        //设置文字颜色
        vc.tabBarItem.setTitleTextAttributes([NSAttributedStringKey.foregroundColor : UIColor.orange], for: .selected)

        //设置文字大小
//        vc.tabBarItem.setTitleTextAttributes([NSAttributedStringKey.font : 10], for: .normal)
        //设置圆角数字
//        vc.tabBarItem.badgeColor = #colorLiteral(red: 1, green: 0.1491314173, blue: 0, alpha: 1)
//        vc.tabBarItem.badgeValue = "99" //当网络数据值没有的时候就置为nil
        //文字偏移
//        vc.tabBarItem.titlePositionAdjustment = UIOffsetMake(10, 10)
        //图片偏移
//        vc.tabBarItem.imageInsets = UIEdgeInsetsMake(2, 2, 2, 2)
        let nav = WWNavigationController(rootViewController: vc)
        self.addChildViewController(nav)
    
    }

    override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {

//        print("///\(item.tag)")
        var index = 0
        for sbView in tabBar.subviews {
            if sbView.isKind(of: NSClassFromString("UITabBarButton")!){
                if index == item.tag{ //用index记录一下,不然会打印四次
//                    print("///\(sbView)")
                    for v in sbView.subviews {
                        if v.isKind(of: NSClassFromString("UIImageView")!){
//                           print("///\(v)")
                            v.transform = CGAffineTransform.init(scaleX: 0.6, y: 0.6)
                            UIView.animate(withDuration: 0.6, delay: 0, usingSpringWithDamping: 0.3, initialSpringVelocity: 6, options: [], animations: {
                                v.transform = CGAffineTransform.identity
                            }, completion: { (_) in
                                
                            })
                            
                        }
                    }
                }
                index += 1
            }
        }
    }
    

}
